using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Xml.Linq;
using System.Globalization;
using System.Net.NetworkInformation;

namespace LyonMag
{
    public partial class PollResults : PhoneApplicationPage
    {
        public PollResults()
        {
            InitializeComponent();
            GetPoll();
        }

      
        public void GetPoll()
        {
            try
            {
                WebClient wc = new WebClient();
                wc.DownloadStringAsync(new Uri("http://www.lyonmag.com/win8/poll_answer"));
                wc.DownloadStringCompleted += wc_DownloadStringCompleted;
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }
        void wc_DownloadStringCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e)
        {
            try
            {
                string peopleXMLPath = e.Result.ToString();
                XDocument loadedData = XDocument.Parse(peopleXMLPath);

                var data = from query in loadedData.Elements("lyon_mag").Elements("poll")
                           select new PollClass.PollMain
                           {
                               Question = (string)query.Element("question"),

                               Id = (string)query.Element("id"),


                               Poll_Responses = new List<PollClass.PollResponses>(from sz in query.Elements("reponses").Elements("reponse")
                                                                                  select new PollClass.PollResponses()
                                                                                  {
                                                                                      Reponse_txt = (string)sz.Element("reponse_txt"),
                                                                                      Reponse_type = (string)sz.Element("reponse_type"),
                                                                                      Reponse_id = (string)sz.Element("reponse_id"),
                                                                                      Reponse_resultat = (string)sz.Element("reponse_resultat")

                                                                                  }).ToList()




                           };

                foreach (PollClass.PollMain dat in data)
                {
                    txtPollQuestion.Text = dat.Question.ToString();

                   Opt1.Value = double.Parse(dat.Poll_Responses[0].Reponse_resultat, CultureInfo.InvariantCulture);
                    Opt2.Value =double.Parse(dat.Poll_Responses[1].Reponse_resultat, CultureInfo.InvariantCulture);
                    Opt3.Value = double.Parse(dat.Poll_Responses[2].Reponse_resultat, CultureInfo.InvariantCulture);
                    Opt4.Value = double.Parse(dat.Poll_Responses[3].Reponse_resultat, CultureInfo.InvariantCulture);
                    txt1.Text = Math.Round(double.Parse(dat.Poll_Responses[0].Reponse_resultat, CultureInfo.InvariantCulture),2).ToString() + " % : " + dat.Poll_Responses[0].Reponse_txt;
                    txt2.Text = Math.Round(double.Parse(dat.Poll_Responses[1].Reponse_resultat, CultureInfo.InvariantCulture), 2).ToString() + "% : " + dat.Poll_Responses[1].Reponse_txt;
                    txt3.Text = Math.Round(double.Parse(dat.Poll_Responses[2].Reponse_resultat, CultureInfo.InvariantCulture), 2).ToString() + "% : " + dat.Poll_Responses[2].Reponse_txt;
                    txt4.Text = Math.Round(double.Parse(dat.Poll_Responses[3].Reponse_resultat, CultureInfo.InvariantCulture), 2).ToString() + "% : " + dat.Poll_Responses[3].Reponse_txt;
                }
                prgs.Visibility = Visibility.Collapsed;
            }
            catch (Exception ex)
            {
               ex.Message.ToString();

               if (NetworkInterface.GetIsNetworkAvailable() == true)
               {
                   // MessageBox.Show(NetworkInterface.GetIsNetworkAvailable().ToString());
               }
               else
               {
                   MessageBox.Show("Internet Not Availabe", "LyonMag", MessageBoxButton.OK);
               }
            }
        }
    }
}